package com.gjszn.kh.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.gjszn.kh.model.Member;
import com.gjszn.kh.service.MemberService;
import com.gjszn.kh.utils.CheckUtil;
import com.gjszn.kh.utils.DateUtil;

/**    
* @Title: UserController.java  
* @Package com.gjszn.kh.controller.admin    
* @author jys@nx-yt.com    
* @date 2016年1月5日 下午3:46:10  
* @version V1.0    
* @Description: TODO(用一句话描述该文件做什么)
********************************************/
@Controller
@RequestMapping("/admin/members")
public class MemberController {
	
	private static Logger log = LogManager.getRootLogger();
	
	@Autowired
	private MemberService memberService;
	
	@RequestMapping("")
	public String toUsersView(HttpServletRequest request){
//		JSONObject jsonObject = new JSONObject();
//		jsonObject.put("total", MemberContext.getMemberMap().keySet().size());
//		JSONArray array = new JSONArray();
//		for(String key : MemberContext.getMemberMap().keySet()){
//			array.add(JSON.toJSON(MemberContext.getMemberMap().get(key)));
//		}
//		jsonObject.put("rows", array);
//		request.setAttribute("members", jsonObject.toJSONString());
		return "admin/members";
	}
	
	@RequestMapping(value="load", method=RequestMethod.POST)
	public @ResponseBody JSONObject load(int page, int rows){
		log.info("查询会员单位信息，从第" + page + "页，之后的" + rows + "行数据");
		List<Member> members = memberService.listForPage(page, rows);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("total", memberService.total());
		jsonObject.put("rows", members);
		return jsonObject;
	}
	
	@RequestMapping(value="add", method=RequestMethod.POST)
	public @ResponseBody JSONObject add(String number, String name, String url, String city, String phone, String remark){
		JSONObject jsonObject = new JSONObject();
		if(CheckUtil.isEmpty(number, name, url, city, phone)){
			jsonObject.put("status", -1);
			jsonObject.put("msg", "数据不能为空");
			return jsonObject;
		}
		Member member = new Member(number, name, url, phone, city, remark, DateUtil.getNowDateTime());
		if(memberService.addOrUpdate(member)){
			jsonObject.put("status", 1);
			jsonObject.put("msg", "处理成功");
		}else{
			jsonObject.put("status", 0);
			jsonObject.put("msg", "处理失败");
		}
		return jsonObject;
	}
	
	
	@RequestMapping(value="delete", method=RequestMethod.POST)
	public @ResponseBody JSONObject delete(String number){
		JSONObject jsonObject = new JSONObject();
		if(CheckUtil.isEmpty(number)){
			jsonObject.put("status", -1);
			jsonObject.put("msg", "数据不能为空");
			return jsonObject;
		}
		if(memberService.delete(number)){
			jsonObject.put("status", 1);
			jsonObject.put("msg", "处理成功");
		}else{
			jsonObject.put("status", 0);
			jsonObject.put("msg", "处理失败");
		}
		return jsonObject;
	}
	
}

